﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Aij1.Utils;
using Aij1.BLL;
using System.Text.RegularExpressions;
using System.Data.Common;

namespace Aij1.Cms
{
    public partial class Main : AdminPage
    {
        DbParameter dateP;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                statDate = dtQueryDate.Date;
            }
            else
            {
                dtQueryDate.Date = statDate;
            }

            dateP = DBHelper.Comm.CreateParameter("@StatDate", statDate);
            cbxTheme.Items.Clear();
            cbxTheme.Items.Add("");
            DirectoryInfo dirInfo = new DirectoryInfo(MapPath("~/App_Themes"));

            foreach (DirectoryInfo dir in dirInfo.GetDirectories())
            {
                cbxTheme.Items.Add(dir.Name);
            }

            cbxTheme.Value = Theme;

            pgDBSize.Position = (int)DBHelper.DefaultHelper.GetDBSize();
            pgSiteSize.Position = siteSize / 1024;
        }

        const int diskBlock = 4096;
        static int diskImgCount = 0;
        static int diskImgSize = 0;
        static int siteSize = 0;

        DateTime statDate = DateTime.Today;
        private int GetSiteSize()
        {
            return siteSize;
        }

        protected int GetDiskImgCount()
        {
            return diskImgCount;
        }

        protected int GetDiskImgSize()
        {
            return diskImgSize / 1024;
        }

        protected int GetCommentCount(bool all)
        {
            return (int)DBHelper.DefaultHelper.ExecGetObject("SELECT COUNT(*) FROM A_Comment"
                + (all ? "" : " WHERE EditTime>=@StatDate"), dateP);
        }

        protected int GetNoteCount(bool all)
        {
            return (int)DBHelper.DefaultHelper.ExecGetObject("SELECT COUNT(*) FROM G_GuestBook"
                                + (all ? "" : " WHERE WriteTime>=@StatDate"), dateP);

        }

        protected int GetDBImgCount(bool all)
        {
            return (int)DBHelper.DefaultHelper.ExecGetObject("SELECT COUNT(*) FROM A_Photo"
                               + (all ? "" : " WHERE EditTime>=@StatDate"), dateP);

        }

        protected int GetDBArticleCount(bool all)
        {
            return (int)DBHelper.DefaultHelper.ExecGetObject("SELECT COUNT(*) FROM A_Article"
                                  + (all ? "" : " WHERE EditTime>=@StatDate"), dateP);
        }

        protected int GetCustomerCount(bool all)
        {
            return (int)DBHelper.DefaultHelper.ExecGetObject("SELECT COUNT(*) FROM C_Customer c INNER JOIN S_Person p ON c.OID=p.OID"
                                  + (all ? "" : " WHERE RegisterTime>=@StatDate"), dateP);
        }

        protected int GetOrderCount(bool all)
        {
            return (int)DBHelper.DefaultHelper.ExecGetObject("SELECT COUNT(*) FROM B_Order"
                                  + (all ? "" : " WHERE OrderTime>=@StatDate"), dateP);
        }

        private int GetDirSize(DirectoryInfo dInfo)
        {
            int size = 0;
            Regex reg = new Regex(@"^\d{9,}(_s)?.[a-z]{3,4}$");
            Regex reg1 = new Regex(@"^\d{9,}.[a-z]{3,4}$");
            foreach (FileInfo fi in dInfo.GetFiles())
            {
                int fsize = (int)(fi.Length / diskBlock + Math.Sign(fi.Length % diskBlock)) * 4;
                if (reg.IsMatch(fi.Name))
                {
                    diskImgSize += fsize;
                }
                if (reg1.IsMatch(fi.Name))
                {
                    diskImgCount++;
                }
                size += fsize;
            }
            foreach (DirectoryInfo di in dInfo.GetDirectories())
            {
                size += GetDirSize(di);
            }
            return size;
        }

        protected void btnPurgeData_Click(object sender, EventArgs e)
        {
            var result = XpoSession.PurgeDeletedObjects();
            lblTips.Text = String.Format("共处理{0}条数据，清除成功{1}条数据,有{2}条数据未清除。", result.Processed, result.Purged, result.NonPurged);
        }

        protected void btnShrinkDB_Click(object sender, EventArgs e)
        {
            DBHelper.DefaultHelper.ShrinkDB();
            pgDBSize.Position = (int)DBHelper.DefaultHelper.GetDBSize();
        }

        protected void btnReCalc_Click(object sender, EventArgs e)
        {
            DirectoryInfo dInfo = new DirectoryInfo(Server.MapPath("~/"));
            diskImgCount = 0;
            diskImgSize = 0;
            siteSize = GetDirSize(dInfo);
            pgSiteSize.Position = siteSize / 1024;
        }
    }
}